package net.huashitong.Indicators.app;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.core.web.model.Ztree;
import com.sirdc.modules.sys.entity.SysFile;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysFileService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.AppMessage;
import net.huashitong.Indicators.entity.AppMsgGroup;
import net.huashitong.Indicators.filter.AppMessageFilter;
import net.huashitong.Indicators.service.AppMessageService;
import net.huashitong.Indicators.service.AppMsgGroupService;
import net.huashitong.Indicators.utils.DataTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;


@Controller
@RequestMapping(value = "app/appMsg")
public class AppMessageController extends JsonBaseController {

    @Autowired
    private AppMessageService appMessageService;
    @Autowired
    private AppMsgGroupService appMsgGroupService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysFileService fileService;

    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 发送消息
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAppMsg", method = RequestMethod.POST)
    public Message addAppMsg(@RequestBody AppMessageFilter filter) {
        if (ObjectUtils.isBlank(filter.getUserIds())) {
            return coverMessage("500", "没有填写收件人");
        }
        String userIdsStr = filter.getUserIds();
        String[] users = userIdsStr.split(",");
        List<String> userIds = Arrays.asList(users);

        String shorts = filter.getShortUserIds();
        String[] shortsArr = shorts.split(",");
        List<String> shortUserIds = Arrays.asList(shortsArr);
        //发送消息
        boolean isSuccess = appMessageService.addSendMsg(userIds, filter, shortUserIds);
        if (isSuccess) {
            return coverMessage("200", "发送成功");
        } else {
            return coverMessage("500", "发送失败");
        }
    }


    @ResponseBody
    @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
    public Message addAppMsg(@RequestParam MultipartFile files) {
        if (ObjectUtils.isNotBlank(files)) {
            try {
                SysFile sysfile = new SysFile();
                fileService.saveFile(sysfile, files);
                String picture = sysfile.getUrl();
                Map map = new HashMap();
                map.put("picture",picture);
                return coverMessage("200", "图片上传成功", map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return coverMessage("200", "没有图片");
    }


    /**
     * 阅读消息
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/readAppMsg", method = RequestMethod.POST)
    public Message readAppMsg(@RequestBody AppMessageFilter filter) {
        AppMessage appMessage = appMessageService.getById(filter.getSysId());
        appMessage.setReadStatus("1");
        appMessageService.update(appMessage);
        Map<String, String> map = new HashMap<>();
        map.put("msgText", appMessage.getMsgText());
        map.put("msgTitle", appMessage.getMsgTitle());
        map.put("picture", appMessage.getPicture());
        map.put("readStatus", appMessage.getReadStatus());
        map.put("source", appMessage.getSource());
        SysUser sender = sysUserService.getById(appMessage.getSender());                //发送方
        SysUser receiver = sysUserService.getById(appMessage.getReceiver());            //接收方
        map.put("sender", sender.getName());
        map.put("receiver", receiver.getName());
        map.put("source", appMessage.getSender());
        return coverMessage("200", "查询成功", map);
    }

    /**
     * 查看消息列表
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryAppMsgList", method = RequestMethod.POST)
    public Message queryAppMsgList(@RequestBody AppMessageFilter filter) {
        List<AppMessage> appMsgs = appMessageService.queryByUserIdOrderCreateDate(filter);
        List<Map<String, String>> result = new ArrayList<>();
        for (AppMessage msg : appMsgs) {
            Map<String, String> map = new HashMap<>();
            map.put("msgText", msg.getMsgText().substring(0, 40));
            map.put("msgTitle", msg.getMsgTitle());
            map.put("readStatus", msg.getReadStatus());
            map.put("source", msg.getSource());
            map.put("picture", msg.getPicture());
            map.put("createDate", msg.getCreateDate());
            result.add(map);
        }
        return coverMessage("200", "查询成功", result);
    }


    /**
     * 查询部门树
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDeptTree", method = RequestMethod.POST)
    public Message queryDeptTree(@RequestBody AppMessageFilter filter) {
        List<Map<String, Object>> result = new ArrayList<>();
        //查询分组（关联发送方用户）
        List<AppMsgGroup> groups = appMsgGroupService.queryByFilter(filter);
        List<Map<String, Object>> groupList = appMessageService.queryWithMsgGroup2(groups);
        Map<String, Object> groupMap = new HashMap<>();
        groupMap.put("id", "group");
        groupMap.put("name", "自定义分组");
        groupMap.put("pid", "");
        groupMap.put("child", groupList);
        result.add(groupMap);
        //所有部门
        List<Ztree> trees = sysDeptService.queryDept2Combotree("");
        List<Map<String, Object>> result1 = appMessageService.getTree(trees);
        result.addAll(result1);
        return coverMessage("200", "获取成功", result);
    }

    /**
     * 查询分组
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryGrouping", method = RequestMethod.POST)
    public Message queryGrouping(@RequestBody AppMessageFilter filter) {
        List<Map<String, Object>> result;
        if (StringUtils.isBlank(filter.getGroupSysId())) {
            result = appMessageService.queryAllMsgGroup();
        } else {
            AppMsgGroup group = appMsgGroupService.getById(filter.getGroupSysId());
            List<AppMsgGroup> groups = new ArrayList<>();
            groups.add(group);
            result = appMessageService.dealWithMsgGroup(groups);
        }
        return coverMessage("200", "获取成功", result);
    }


    /**
     * 新建分组
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createGrouping", method = RequestMethod.POST)
    public Message createGrouping(@RequestBody AppMessageFilter filter) {
        AppMsgGroup group = new AppMsgGroup();
        if (StringUtils.isBlank(filter.getGroupName())) {
            filter.setGroupName("新建分组-" + DataTimeUtils.getCurrentDateTime());
        }
        String userIds = filter.getUserIds();
        String userIdss = userIds.replace(" ", "");
        group.setGroupName(filter.getGroupName());
        group.setUserIds(userIdss);
        group.setOpenId(filter.getOpenId());
        appMsgGroupService.save(group);
        return coverMessage("200", "分组创建成功");
    }

}
